﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
    using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.CBO.Pub.Controller;
    using UFIDA.U9.SM.Ship;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Util.Context;

	/// <summary>
	/// ShipRevocateSV partial 
	/// </summary>	
	public partial class ShipRevocateSV 
	{	
		internal BaseStrategy Select()
		{
			return new ShipRevocateSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class ShipRevocateSVImpementStrategy : BaseStrategy
	{
		public ShipRevocateSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			ShipRevocateSV bpObj = (ShipRevocateSV)obj;

            try
            {
                if (bpObj.Data == null || bpObj.Data.Count == 0)
                    throw new Exception("传入参数为空");
                List<CommonArchiveDataDTOData> retlist = new List<CommonArchiveDataDTOData>();

                foreach (var dto in bpObj.Data)
                {
                    Ship item = Ship.Finder.Find($"DocNo='{dto.DocNo}'");
                    if (item == null)
                    {
                        //新增逻辑
                        throw new Exception($"单号:{dto.DocNo}不存在!");
                    }
                    else
                    {
                        //修改逻辑
                        if ( item.Status != ShipStateEnum.Approving)
                            throw new Exception($"单号[{item.DocNo}]状态不为审核中");

                        using (ISession session = Session.Open())
                        {
                            item.Status = ShipStateEnum.Creates;

                            session.Commit();
                        }

                        CommonArchiveDataDTOData ret = new CommonArchiveDataDTOData()
                        {
                            ID = item.ID,
                            Code = item.DocNo,
                        };
                        retlist.Add(ret);
                    }
                }
                return ResultUtils.success(bpObj, bpObj.Data, retlist);
            }
            catch (Exception ex)
            {
                return ResultUtils.error(bpObj, bpObj.Data, ex);
            }
        }		
	}

	#endregion
	
	
}